import axios from 'axios'

export default {
    namespaced: true,
    state: {
        dailyStats: {},
        refundOrders: [],
        pagination: {
            currentPage: 1,
            pageSize: 10,
            total: 0
        },
        orderPagination: {
            currentPage: 1,
            pageSize: 10,
            total: 0
        },
        orders: [],
        searchForm: {
            orderId: '',
            status: '',
            type: -4
        },
        refundSearchForm: {
            type: -1,
            orderId: '',
        },
    },
    mutations: {
        SET_PAGE_SIZE(state, stats){
            state.pagination.page = stats
        },
        SET_CURRENT_PAGE(state, stats){
            state.pagination.limit = stats
        },
        SET_REFUND_TOTAL(state, total) {
            state.pagination.total = total
        },
        SET_DAILY_STATS(state, stats) {
            state.dailyStats = stats
        },
        SET_REFUND_ORDERS(state, orders) {
            state.refundOrders = orders
        },
        SET_ORDER_PAGE_SIZE(state, size) {
            state.orderPagination.pageSize = size
        },
        SET_ORDER_CURRENT_PAGE(state, page) {
            state.orderPagination.currentPage = page
        },
        SET_ORDER_TOTAL(state, total) {
            state.orderPagination.total = total
        },
        SET_ORDERS(state, orders) {
            state.orders = orders
        },
        SET_SEARCH_FORM(state, searchForm) {
            state.searchForm = { ...state.searchForm, ...searchForm }
        },
        SET_REFUND_SEARCH_FORM(state, refundSearchForm) {
            state.refundSearchForm = { ...state.refundSearchForm, ...refundSearchForm }
        },
    },
    actions: {
        async fetchDailyStats({ commit }) {
            try {
                const response = await axios.get('admin/api/admin/daily-stat')
                if (response.data.code === 200) {
                    if (response.data.data === null){
                        return
                    }
                    const stats = response.data.data
                    // 将日期字符串转换为Date对象
                    stats.statDate = new Date(stats.statDate)
                    stats.createdAt = new Date(stats.createdAt)
                    stats.updatedAt = new Date(stats.updatedAt)
                    commit('SET_DAILY_STATS', stats)
                } else {
                    throw new Error(response.data.message || '获取数据失败')
                }
            } catch (error) {
                console.error('获取今日统计数据失败:', error)
                throw error
            }
        },
        async fetchRefundOrders({ commit, state } ) {
            try {
                const response = await axios.get('admin/api/admin/refund-orders', {
                    params: {
                        type: state.refundSearchForm.type,
                        orderId: state.refundSearchForm.orderId,
                        page: state.pagination.currentPage,
                        limit: state.pagination.pageSize
                    }
                })
                if (response.data.code === 200) {
                    commit('SET_REFUND_ORDERS', response.data.data.list)
                    commit('SET_REFUND_TOTAL',response.data.data.total)
                    commit('SET_ORDER_CURRENT_PAGE',response.data.data.page)
                    commit('SET_PAGE_SIZE',response.data.data.limit)
                } else {
                    throw new Error(response.data.message || '获取退款订单失败')
                }
            } catch (error) {
                console.error('获取退款订单失败:', error)
                throw error
            }
        },
        async orders({ commit, state }) {
            try {
                const response = await axios.get('admin/api/admin/orders', {
                    params: {
                        type: state.searchForm.type,
                        orderId: state.searchForm.orderId,
                        page: state.orderPagination.currentPage,
                        limit: state.orderPagination.pageSize,
                    }
                })
                if (response.data.code === 200) {
                    commit('SET_ORDERS', response.data.data.list)
                    commit('SET_ORDER_TOTAL',response.data.data.total)
                    commit('SET_CURRENT_PAGE',response.data.data.page)
                    commit('SET_ORDER_PAGE_SIZE',response.data.data.limit)
                } else {
                    throw new Error(response.data.message || '获取订单失败')
                }
            } catch (error) {
                console.error('获取订单失败:', error)
                throw error
            }
        },
        updateSearchForm({ commit }, searchForm) {
            commit('SET_SEARCH_FORM', searchForm)
        },
        updateRefundSearchForm({ commit }, refundSearchForm) {
            commit('SET_REFUND_SEARCH_FORM', refundSearchForm)
        },
        async approveRefund(_, { id, orderId }) {
            try {
                await axios.post(`admin/api/admin/approve-refund/${id}&${orderId}`)
                return true
            } catch (error) {
                console.error('批准退款失败:', error)
                throw error
            }
        },

        async rejectRefund(_, { id, orderId, reason }) {
            try {
                await axios.post(`admin/api/admin/reject-refund/${id}&${orderId}`, { reason })
                return true
            } catch (error) {
                console.error('拒绝退款失败:', error)
                throw error
            }
        }
    },
    getters: {
        getDailyStats: (state) => state.dailyStats,
        getRefundOrders: state => state.refundOrders,
        getOrders: state => state.orders,
        getSearchForm: state => state.searchForm,
        getRefundSearchForm: state => state.refundSearchForm
    },
}
